﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2011
*/
#region
using System;
using System.Data;
using System.ComponentModel;
using NetBay.Core.Log;
using NetBay.Core.DataAccess;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Extensions;
using NetBay.Core.BusinessEntities.Enum;

#endregion

namespace Rio.Manager.DataAccess
{
    /// <summary>
    /// DataAcces for Dashboard
    /// </summary>
    public class DA_GeneralParameter
    {
        #region Enums
        public enum DbFunction
        {
            [DescriptionAttribute("")]

            None,
            [DescriptionAttribute("P_ADMIN_PARAMETER_GetAll")]

            GetAllParameter,
            [DescriptionAttribute("P_ADMIN_PARAMETER_Get")]

            GetParameter,
            [DescriptionAttribute("P_ADMIN_PARAMETER_Update")]

            UpdateParameter,
            [DescriptionAttribute("P_ADMIN_PARAMETER_Add")]

            AddParameter,
        }

        #endregion

        #region Function

        /// <summary>
        /// Get all parameters
        /// </summary>
        /// <returns></returns>
        public static IDataReader GetAllGeneralParameters()
        {
            try
            {
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.GetAllParameter.szGetDescription());

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("GetAllGeneralParameters exception[" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Get one parameter
        /// </summary>
        /// <param name="generalParameterName"></param>
        /// <returns></returns>
        public static IDataReader GetGeneralParameter(GeneralParameterName generalParameterName)
        {
            try
            {
                LogManager.Instance().LogDebug("( szGetDescription<" + generalParameterName.szGetDescription() + ">)");
                
                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.GetParameter.szGetDescription());
                db.AddInParameter(dbCommand, "p_sParamName", DbType.String, generalParameterName.szGetDescription());

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("GetGeneralParameter exception[" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Update a general parameter
        /// </summary>
        /// <param name="updatedGeneralParameter"></param>
        /// <returns></returns>
        public static bool UpdateGeneralParameter(GeneralParameter updatedGeneralParameter)
        {
            try
            {
                LogManager.Instance().LogDebug("( " + updatedGeneralParameter.ToString() + ")");
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.UpdateParameter.szGetDescription(), false);
                db.AddInParameter(dbCommand, "p_lIdentifier", DbType.Int64, updatedGeneralParameter.Identifier);
                db.AddInParameter(dbCommand, "p_sName", DbType.String, updatedGeneralParameter.Name);
                db.AddInParameter(dbCommand, "p_sDescription", DbType.String, updatedGeneralParameter.Description);
                db.AddInParameter(dbCommand, "p_sValue", DbType.String, updatedGeneralParameter.Value);
                db.AddInParameter(dbCommand, "p_sDomain", DbType.String, updatedGeneralParameter.Domain.szGetDescription());

                db.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("UpdateGeneralParameter exception[" + ex.Message + "]");
                return false;
            }
        }

        /// <summary>
        /// Adds a general parameter
        /// </summary>
        /// <param name="updatedGeneralParameter"></param>
        /// <returns></returns>
        public static bool AddGeneralParameter(GeneralParameter addGeneralParameter)
        {
            try
            {
                LogManager.Instance().LogDebug("( " + addGeneralParameter.ToString() + ")");
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.AddParameter.szGetDescription(), false);
                db.AddInParameter(dbCommand, "p_sName", DbType.String, addGeneralParameter.Name);
                db.AddInParameter(dbCommand, "p_sDescription", DbType.String, addGeneralParameter.Description);
                db.AddInParameter(dbCommand, "p_sValue", DbType.String, addGeneralParameter.Value);
                db.AddInParameter(dbCommand, "p_sDomain", DbType.String, addGeneralParameter.Domain.szGetDescription());

                db.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("AddGeneralParameter exception[" + ex.Message + "]");
                return false;
            }
        }
        #endregion
    }
}